package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.BackgroundImageCheckDTO;
import com.poetize.plus.entity.dto.BackgroundImageDTO;
import com.poetize.plus.entity.dto.page.BackgroundImagePageDTO;
import com.poetize.plus.entity.vo.page.BackgroundImagePageVO;
import com.poetize.plus.entity.vo.UploadVO;
import com.poetize.plus.service.BackgroundImageService;
import com.poetize.plus.service.QiNiuService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zuosy
 * @created 2024/4/1 9:15
 */
@Tag(name = "资源信息")
@RestController
@RequestMapping("/admin/resource")
@RequiredArgsConstructor
public class ResourceController {

    private final QiNiuService qiNiuService;

    private final BackgroundImageService backgroundImageService;

    @PostMapping("/uploadQiNiu")
    @Operation(summary = "七牛云上传文件")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<UploadVO> uploadQiNiu(@RequestParam("file") MultipartFile file){
        return R.ok(qiNiuService.upload(file));
    }

    @GetMapping("/backgroundImage/page")
    @Operation(summary = "背景图分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<BackgroundImagePageVO>> imagePage(BackgroundImagePageDTO backgroundImagePageDTO){
        return R.ok(backgroundImageService.imagePage(backgroundImagePageDTO));
    }

    @GetMapping("/backgroundImage/checkImage")
    @Operation(summary = "背景图校验是否可以上传")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> checkImage(BackgroundImageCheckDTO backgroundImageCheckDTO){
        return R.ok(backgroundImageService.checkImage(backgroundImageCheckDTO));
    }

    @PostMapping("/backgroundImage/add")
    @Operation(summary = "背景图保存")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> add(@RequestBody @Validated BackgroundImageDTO backgroundImageDTO){
        return R.ok(backgroundImageService.add(backgroundImageDTO));
    }

    @DeleteMapping("/backgroundImage/{id}")
    @Operation(summary = "背景图删除")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> delete(@PathVariable("id") Long id){
        return R.ok(backgroundImageService.removeById(id));
    }

}
